﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SmartAPI.FileReader;
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

namespace GeneralUploadFile
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var services = new ServiceCollection();
            var jsonOptions = new JsonSerializerOptions
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };
            services.AddHttpApi<IFileReaderApi>()
               .ConfigureHttpApi(o =>
               {
                   o.HttpHost = new Uri("http://211.155.90.106:9081");
                   o.JsonSerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                   o.JsonSerializeOptions.IgnoreNullValues = true;
                   o.JsonDeserializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
               });
            services.AddLogging(o =>
            {
                o.AddConsole();
            });
            var serviceProvider = services.BuildServiceProvider();

            var fileReadApi = serviceProvider.GetService<IFileReaderApi>();
            var uploadResult = await fileReadApi.ReadFileAsync(new WebApiClientCore.Parameters.FormDataFile("sample.pdf"));
            Console.WriteLine("文件上传结果");
            Console.WriteLine(JsonSerializer.Serialize(uploadResult, jsonOptions));

            await Task.Delay(5000);

            var taskResult = await fileReadApi.GetResultAsync(uploadResult.Data);
            Console.WriteLine("查询任务结果");
            Console.WriteLine(JsonSerializer.Serialize(taskResult, jsonOptions));

            var bytes = await fileReadApi.GetResultFileAsync(uploadResult.Data);
            await File.WriteAllBytesAsync("download.pdf", bytes);
            Console.WriteLine("download.pdf 下载完成");

        }
    }
}
